package com.eschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eschool.mapper.UserMapper;
import com.eschool.pojo.CourseDAO;
import com.eschool.pojo.UserChangeDTO;
import com.eschool.pojo.UserDAO;
import com.eschool.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public List<UserDAO> findAllUser() {
        List<UserDAO> userDAOS;
        userDAOS = userMapper.selectList(null);
        return userDAOS;
    }

    @Override
    public UserDAO Login(String password,Integer stuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("password",password);
        queryWrapper.eq("stuid",stuid);

        UserDAO userDAO = userMapper.selectOne(queryWrapper);
        return userDAO;
    }

    @Override
    public UserDAO findById(Integer stuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuid",stuid);
        UserDAO userDAO = userMapper.selectOne(queryWrapper);
        return userDAO;
    }

    @Override
    public int UserChangeInfo(UserChangeDTO userChangeDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuid",userChangeDTO.getStuid());
        UserDAO userDAO = userMapper.selectOne(queryWrapper); //先查出来要修改的用户

        BeanUtils.copyProperties(userChangeDTO,userDAO);
        int updaterow = userMapper.update(userDAO,queryWrapper);
        return updaterow;
    }

    @Override
    public int UserChangePassword(Integer stuid, String old_password, String new_password) {
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("stuid",stuid);
        UserDAO userDAO = userMapper.selectOne(queryWrapper1);

        if(userDAO.getPassword().equals(old_password)){
            userDAO.setPassword(new_password);
            int updateRow = userMapper.update(userDAO,queryWrapper1);
            return updateRow;
        }
        else{
            return -1;
        }
    }


}
